const {
  getOrder,
  getOrderList,
  dbList,
  getdbsqb
} = require('../../utils/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //权限切换 1司机 2站点
    quanxian: '',
    activeIndex: 0,
    tabs: [{
        title: '全部',
        status: 'all'
      },
      {
        title: '待接单',
        status: 'pending'
      },
      {
        title: '运输中',
        status: 'shipping'
      }
    ],
    ordersList: [],
    orders: [{
        id: 1,
        orderNo: 'LOG2023080001',
        status: 'pending',
        statusText: '待接单',
        from: '上海浦东仓库',
        to: '徐汇区收货点',
        price: 150,
        distance: 15.5
      },
      {
        id: 1,
        orderNo: 'LOG2023080001',
        status: 'pending',
        statusText: '待接单',
        from: '上海浦东仓库',
        to: '徐汇区收货点',
        price: 150,
        distance: 15.5
      }
    ],
    orderList: []
  },
  getList() {
    const data = {
      page: 1,
      pageSize: 20,
      appends: "r_pd,f_driver,f_driver.f_car_team,f_a2u1smgkzwg"
    }
    getOrderList(data).then(res => {
      this.setData({
        orderList: res.data
      })
    })
  },
  viewDetail(e) {
    const id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: `/pages/list/detail?id=${id}`
    })
  },

  onTabChangesiji(e) {
    const index = e.detail.index
    console.log(index)
    this.setData({
      activeIndex: index
    })
    this.loadOrders(this.data.tabs[index].status)
    if (index == 0) {
      this.getOrderList() //待接单
    } else {
      this.getOrderList2() //待接单
    }
  },
  onTabChange(e) {
    const index = e.detail.index
    console.log(index)
    this.setData({
      activeIndex: index
    })
    this.loadOrders(this.data.tabs[index].status)
    if (index == 0) {
      this.getDbList()
    } else {
      this.getDbList2()
    }
  },

  loadOrders(status) {
    // 根据状态加载运单数据
    console.log('加载状态:', status)
  },
  //获取调拨数据列表
  getDbList() {
    const data = {
      pageSize: 20,
      sort:'-id',
      appends: 'f_23ky2x5rnzi,f_hljt1f72ryh,f_sqszdmc,f_sqszdmc.parent(recursively=true)',
      filter: {"$or":[{"f_shzt":{"$eq":"2"}},{"f_shzt":{"$eq":"3"}}]}
    }
    console.log(data)
    dbList(data).then(res => {
      this.setData({
        ordersList: res.data
      })
    }).catch(res => {

    })
  },
  getDbList2() {
    const data = {
      sort:'-id',
      pageSize: 20,
      appends: 'f_hljt1f72ryh,f_23ky2x5rnzi,f_sqszdmc,f_sqszdmc.parent(recursively=true)',
      filter: {"$or":[{"f_shzt":{"$eq":"2"}},{"f_shzt":{"$eq":"3"}}]}
    }
    console.log('看数据',data)
    dbList(data).then(res => {
      this.setData({
        ordersList: res.data
      })
    }).catch(res => {

    })
  },
  //派车列表待接单
  getOrderList() {
    const data = {
      page: '1',
      pageSize: '20',
      appends: "f_team",
      filter: {
        "$and": [{
          "f_status": {
            "$eq": "2"
          }
        }]
      }
    }
    getOrder(data).then(res => {
      this.setData({
        ordersList: res.data
      })
    })
  },
  //派车列表待接单
  getOrderList2() {
    const data = {
      page: '1',
      pageSize: '20',
      appends: "f_team",
      filter: {
        "$and": [{
          "f_status": {
            "$eq": "5"
          }
        }]
      }
    }
    getOrder(data).then(res => {
      this.setData({
        ordersList: res.data
      })
    })
  },
  onTabChangepaiche(e) {
    const index = e.detail.index
    console.log(index)
    this.setData({
      activeIndex: index
    })
    this.loadOrders(this.data.tabs[index].status)
    if (index == 0) {
      this.getOrderList() //待接单
    } else {
      this.getOrderList2() //待接单
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      quanxian: wx.getStorageSync('quanxian'),
      activeIndex: wx.getStorageSync('acitve')
    });
    if (this.data.quanxian == 'siji') {
      this.getList()
      wx.removeStorageSync('acitve')
    } else if (this.data.quanxian == 'zhandian') {
      this.getDbList()
    } else if (this.data.quanxian == 'chedui') {
      this.getOrderList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})